package paper.image;

import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.awt.geom.*;     
import java.util.Vector;
import java.util.Date;
import java.awt.font.TextLayout;
import java.util.Enumeration;
import javax.imageio.*;

import paper.image.ImageHold;
import paper.image.SegmentID;


public class TestImg extends JPanel 
{
	static File inFile;	  
	static Vector areaVector;
	static Vector v;
	static Vector debugv;
	static SegmentID segmentID;
	static ImageHold imageHold;

  	public void paintComponent(Graphics g) {
    		Graphics2D g2d = (Graphics2D)g;
		g2d.drawImage(imageHold.getImg(), 0, 0, this);
		
		
                g2d.setColor(Color.red);
                for (Enumeration e = areaVector.elements(); e.hasMoreElements();) {
                                Rectangle tmpRect=(Rectangle) e.nextElement();
                                g2d.drawRect((int)tmpRect.getX(),(int)tmpRect.getY(),(int)tmpRect.getWidth(),(int)tmpRect.getHeight());

                } 
		Rectangle testRectangle=segmentID.getProbableTitle();
                g2d.setColor(Color.green);
		g2d.drawRect((int)testRectangle.getX(),(int)testRectangle.getY(),(int)testRectangle.getWidth(),(int)testRectangle.getHeight());
                g2d.setColor(Color.black);
                for (Enumeration e = v.elements(); e.hasMoreElements();) {
                                Rectangle tmpRect=(Rectangle) e.nextElement();

                                g2d.drawRect((int)tmpRect.getX(),(int)tmpRect.getY(),(int)tmpRect.getWidth(),(int)tmpRect.getHeight());

                }  
		// debug 
		/* 
		g2d.setColor(Color.red);
		for(Enumeration e2=debugv.elements(); e2.hasMoreElements();){
				Rectangle tmp2Rect=(Rectangle) e2.nextElement();
				g2d.drawRect((int)tmp2Rect.getX(),(int)tmp2Rect.getY(),(int)tmp2Rect.getWidth(),(int)tmp2Rect.getHeight());
		}*/

  	}

  	public static void main(String[] args) {
		if(args.length>0){
			File f = new File(args[0]);

			if(f.exists()){
				inFile=new File(args[0]);
			} else {
				init();
			}
		} else {
			init();
		}
		imageHold=new ImageHold();
		imageHold.viewTest(inFile);
        	// subdivideRectangle(Rectangle subdivideMe, Vector foundRectangles,int maxdepth){
		System.out.println("Image is "+imageHold.getImgWidth() + ", "+imageHold.getImgHeight());
		areaVector=imageHold.getAreas(); 
		v=imageHold.getTempAreas(); 
		debugv=imageHold.getDebugAreas(); 
		segmentID=new SegmentID(areaVector,imageHold.getLeftMargin(), imageHold.getRightMargin(),imageHold.getImgWidth());		
    		WindowUtilities.openInJFrame(new TestImg(), imageHold.getImgWidth(), imageHold.getImgHeight());
  	}



    	public static void init() {

		inFile=new java.io.File("/home/em/Devel2/paperBase/nobackup/pdfarchive/fd/54/fd54214ba61df8d69d2d2f62265fd40c.png");
		inFile=new java.io.File("/home/em/Devel2/paperBase/nobackup/pdfarchive/f4/2c/f42c404d6acbf6c0d86f2dd99f9cace6.png");
		inFile=new java.io.File("/home/em/Devel2/paperBase/nobackup/pdfarchive/c2/40/c2403f2b22bac994705ef3f7da24c610.png");
		inFile=new java.io.File("/home/em/Devel2/paperBase/nobackup/pdfarchive/03/28/0328a860619f727c93e17654684e7cc9.png");
		inFile=new java.io.File("/home/em/Devel2/paperBase/nobackup/pdfarchive/e7/a7/e7a7bc9ffcbbf5462886d94fde4ad260.png");
		// this one is dual column...

		// TODO: Try to subdivide the rects (for dual columns, multiple authors etc)
		// TODO: think about ground truths, zoning, standard formats

		inFile=new java.io.File("/home/em/Devel2/paperBase/nobackup/pdfarchive/d4/a4/d4a46e059e9b934195f14a196a21e930.png");
    	}
		
}


/* Modeline for ViM {{{
 * vim: set ts=4:
 * vim600: fdm=marker fdl=0 fdc=3:
 * }}} */

